package com.yyfax.ph.home;

import android.content.Context;
import com.yyfax.common.parse.JsonMananger;
import com.yyfax.common.CLog;
import com.yyfax.ph.common.location.LocationManager;

import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Title: LocalAreaManager Description: Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年7月14日 上午9:16:58
 */
public class LocalAreaManager {

	private final String TAG = LocationManager.class.getSimpleName();

	private static LocalAreaManager mLocalAreaManager;

	public LocalAreaManager(Context context) {
		AddressBean addressBean = getAddressInfos(context);
		initProvinceCityDistrict(addressBean);
	}

	public static LocalAreaManager getInstance(Context context) {
		if (null == mLocalAreaManager) {
			mLocalAreaManager = new LocalAreaManager(context);
		}
		return mLocalAreaManager;
	}

	public static LocalAreaManager getInstance() {
		return mLocalAreaManager;
	}

	public void clear() {
		mLocalAreaManager = null;
	}

	private String addressFileName = "area.txt";

	public ArrayList<AddressProvinceBean> provinceList = new ArrayList<>();
	public ArrayList<ArrayList<AddressCityBean>> cityList = new ArrayList<ArrayList<AddressCityBean>>();
	public ArrayList<ArrayList<ArrayList<AddressDistrictBean>>> districtList = new ArrayList<ArrayList<ArrayList<AddressDistrictBean>>>();

	public int provinceIndex;
	public int cityIndex;
	public int districtIndex;

	public String province;
	public String city;
	public String district;

	/***
	 * 根据获取的省市 设定在省市区三个选项中 省市的索引
	 *
	 * @param province
	 * @param city
	 */
	public void setProvinceAndCityIndex(String province, String city) {
		this.province = province;
		this.city = city;
		for (int i = 0; i < provinceList.size(); i++) {
			AddressProvinceBean provinceBean = provinceList.get(i);
			if (provinceBean.getName().equalsIgnoreCase(province)) {
				provinceIndex = i;
				break;
			}
		}

		for (int i = 0; i < cityList.get(provinceIndex).size(); i++) {
			AddressCityBean addressCityBean = cityList.get(provinceIndex).get(i);
			if (addressCityBean.getName().equalsIgnoreCase(city)) {
				cityIndex = i;
				break;
			}
		}
		CLog.i(TAG, "provinceIndex" + provinceIndex + "cityIndex" + cityIndex);
	}

	/***
	 * 根据省市区查找省市区的索引值
	 * @param province
	 * @param city
	 * @param district
	 */
	public void setProvinceAndCityIndex(String province, String city, String district) {
		this.province = province;
		this.city = city;
		this.district = district;
		for (int i = 0; i < provinceList.size(); i++) {
			AddressProvinceBean provinceBean = provinceList.get(i);
			if (provinceBean.getName().equalsIgnoreCase(province)) {
				provinceIndex = i;
				break;
			}
		}

		for (int i = 0; i < cityList.get(provinceIndex).size(); i++) {
			AddressCityBean addressCityBean = cityList.get(provinceIndex).get(i);
			if (addressCityBean.getName().equalsIgnoreCase(city)) {
				cityIndex = i;
				break;
			}
		}
		for (int i = 0; i < districtList.get(provinceIndex).get(cityIndex).size(); i++) {
			AddressDistrictBean districtBean = districtList.get(provinceIndex).get(cityIndex).get(i);
			if (districtBean.getName().equalsIgnoreCase(district)) {
				districtIndex = i;
				break;
			}

		}
		CLog.i(TAG, "provinceIndex" + provinceIndex + "cityIndex" + cityIndex + "districtIndex" + districtIndex);
	}

	private void initProvinceCityDistrict(AddressBean addressBean) {
		for (AddressProvinceBean province : addressBean.getAddress()) {
			provinceList.add(province);
			ArrayList<AddressCityBean> cityList = new ArrayList<AddressCityBean>();
			ArrayList<ArrayList<AddressDistrictBean>> districtList = new ArrayList<ArrayList<AddressDistrictBean>>();
			for (AddressCityBean city : province.getChildren()) {
				cityList.add(city);
				ArrayList<AddressDistrictBean> districtList1 = new ArrayList<AddressDistrictBean>();
				for (AddressDistrictBean district : city.getChildren()) {
					districtList1.add(district);
				}
				districtList.add(districtList1);
			}
			this.cityList.add(cityList);
			this.districtList.add(districtList);
		}
	}

	private AddressBean getAddressInfos(Context context) {
		AddressBean addressBean = null;
		String addressJsonStr = readLocalJson(context, this.addressFileName);
		CLog.i(TAG, addressJsonStr);
		try {
			addressBean = JsonMananger.jsonToBean(addressJsonStr, AddressBean.class);
			CLog.i(TAG, addressBean.toString());
			CLog.i(TAG, addressBean.getAddress().get(3).toString());
			CLog.i(TAG, addressBean.getAddress().get(3).getChildren().toString());
		} catch (Exception e) {
		}
		return addressBean;
	}

	private String readLocalJson(Context context, String fileName) {
		String resultString = "";
		InputStream inputStream = null;
		try {
			inputStream = context.getResources().getAssets().open(fileName);
			byte[] buffer = new byte[inputStream.available()];
			inputStream.read(buffer);
			resultString = new String(buffer, "utf-8");
			if (null != inputStream) {
				inputStream.close();
			}
		} catch (Exception e) {
		}
		return resultString;
	}

	public static class AddressBean implements Serializable {

		private static final long serialVersionUID = 5166850928547491503L;
		public List<AddressProvinceBean> address;

		public List<AddressProvinceBean> getAddress() {
			return address;
		}

		public void setAddress(List<AddressProvinceBean> address) {
			this.address = address;
		}

		public AddressBean(List<AddressProvinceBean> address) {
			super();
			this.address = address;
		}

		public AddressBean() {
			super();
		}

		@Override
		public String toString() {
			return "AddressBean [address=" + address + "]";
		}

	}

	public static class AddressCityBean implements Serializable {

		private static final long serialVersionUID = 5166850928547491503L;
		public String name;
		public List<AddressDistrictBean> children;

		public String getPickerViewText() {
			// 这里还可以判断文字超长截断再提供显示
			return name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public AddressCityBean(String name, List<AddressDistrictBean> children) {
			super();
			this.name = name;
			this.children = children;
		}

		public AddressCityBean() {
			super();
		}

		public List<AddressDistrictBean> getChildren() {
			return children;
		}

		public void setChildren(List<AddressDistrictBean> children) {
			this.children = children;
		}

		@Override
		public String toString() {
			return "AddressCityBean [name=" + name + ", children=" + children
					+ "]";
		}

	}

	public static class AddressDistrictBean implements Serializable {
		private static final long serialVersionUID = 5166850928547491503L;

		public String name;

		public String getPickerViewText() {
			// 这里还可以判断文字超长截断再提供显示
			return name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "AddressDistrictBean [name=" + name + "]";
		}

		public AddressDistrictBean(String name) {
			super();
			this.name = name;
		}

		public AddressDistrictBean() {
			super();
		}

	}

	public static class AddressProvinceBean implements Serializable {

		private static final long serialVersionUID = 2027614225679815922L;
		public String name;
		public List<AddressCityBean> children;

		public String getPickerViewText() {
			// 这里还可以判断文字超长截断再提供显示
			return name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public List<AddressCityBean> getChildren() {
			return children;
		}

		public void setChildren(List<AddressCityBean> children) {
			this.children = children;
		}

		public AddressProvinceBean(String name, List<AddressCityBean> children) {
			super();
			this.name = name;
			this.children = children;
		}

		public AddressProvinceBean() {
			super();
		}

		@Override
		public String toString() {
			return "AddressProvinceBean [name=" + name + ", children=" + children
					+ "]";
		}

	}
}
